package com.rayenterprise.kms.widgets;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.rayenterprise.kms.client.Kms4gwt;
import com.rayenterprise.kms.client.remote.GenreRemote;
import com.rayenterprise.kms.client.remote.InfoRemote;
import com.rayenterprise.kms.domain.KGenre;
import com.rayenterprise.kms.domain.KInfo;
import com.rayenterprise.kms.domain.KNotion;

public class InfoEditor extends Composite {
	private RichTextArea infoText = new RichTextArea();
	private VerticalPanel outer = new VerticalPanel();

	private Label lblGenre = new Label("Genre: ");
	private Label lblIsFinished = new Label("Finished? ");
	private Label lblTags = new Label("Tags ");
	private Label lblInfoText = new Label("Info Content");
	private Label lblAction = new Label("Actions");	
	private RadioButton rdoGenre = new RadioButton("genres");
	RadioButton finishedRadioButton = new RadioButton("Finished","Finished");
	RadioButton unFinishedRadioButton = new RadioButton("Finished","Not Finished");
	private TextBox txtTags = new TextBox();
	private Button btnSave = new Button("Save");
	private KNotion notion = new KNotion();
	private KInfo info = new KInfo();
	private KGenre genre = new KGenre();
	private Boolean finished = false;
	private FlexTable grdGenre = new FlexTable();
	Grid grid = new Grid(2,1);
	private CustomizedHP hpGenre = new CustomizedHP();
	private CustomizedHP hpGenre2 = new CustomizedHP();
	private CustomizedHP hpFinished = new CustomizedHP();
	private CustomizedHP hpTags = new CustomizedHP();
	private CustomizedHP hpSubmit = new CustomizedHP();
		
	private NotionNode nn ;	
	private InfoNode in;
	private FlexTable layout = new FlexTable() ;
	private HandlerManager eventBus;
	private FormPanel fPanel = new FormPanel();
	private Map<RadioButton,KGenre> radioGroup = new HashMap<RadioButton,KGenre>();
	
	/**
	 * This constructor take a notion as parameter to create a new info
	 * 
	 * @param notion
	 */
	public InfoEditor(AbstractCustomNode node,HandlerManager eventBus) {
		this.nn = (NotionNode) node;
		this.notion = (KNotion) node.getUserObject();
		this.eventBus = eventBus;
		initCommon();	
		newInitialization();
			
	}
	
	
	/**
	 * This constructor take a info to update a info
	 * @param in
	 * @param eventBus
	 */
	public InfoEditor(InfoNode in,HandlerManager eventBus){
		this.eventBus = eventBus;
		this.in = in;
		this.info = (KInfo) in.getUserObject();		
		initCommon();	
		updateInitialization();
		
	}
	
	private void newInitialization() {	
		hpGenre.add(lblGenre);
		GenreRemote.Util.getInstance().getGList(
				new AsyncCallback<List<KGenre>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(List<KGenre> result) {
						int i = 0;
						int j = 0;
						for (final KGenre g : result) {
							final RadioButton rd = new RadioButton("genres", g
									.getName());							
							rd.setFormValue(String.valueOf(g.getGenreId()));
							radioGroup.put(rd, g);
							rd.addValueChangeHandler(new ValueChangeHandler<Boolean>(){

								@Override
								public void onValueChange(
										ValueChangeEvent<Boolean> event) {
									if(event.getValue()==true){		
										RadioButton r = (RadioButton)event.getSource();
										genre = radioGroup.get(r);										
									}
									
								}});
							
							grdGenre.setWidget(j, i, rd);							
							i++;							
							if (i % 3 == 0) {	
								j++;
								i = 0;
							}
						}

					}
				});
		hpGenre2.add(grdGenre);
		grdGenre.setCellPadding(5);
		grdGenre.setCellSpacing(10);
		
		layout.setWidget(0, 0, lblGenre);
		layout.setWidget(0, 1, grdGenre);
		layout.setWidget(1, 0, lblIsFinished);
		layout.setWidget(1, 1, hpFinished);
		layout.setWidget(2, 0, lblTags);
		layout.setWidget(2, 1, hpTags);			
		layout.setWidget(3, 0, lblAction);
		layout.setWidget(3, 1, btnSave);
			
		btnSave.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				info.setAddDate(new Date());
				info.setGenre(genre);
				info.setIsFinished(finished);
				info.setTags(txtTags.getValue());
				info.setText(infoText.getHTML());
				info.setNotion(notion);
				InfoRemote.Util.getInstance().save(info, new AsyncCallback<KInfo>(){

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());						
					}

					@Override
					public void onSuccess(KInfo result) {
						InfoNode in = new InfoNode(info,eventBus);
						nn.addItem(in);
					}});
				
			}});
		
	}

	private void updateInitialization() {		
		txtTags.setText(info.getTags());
		infoText.setHTML(info.getText());
		if(info.getIsFinished())
			finishedRadioButton.setValue(true);
		
		layout.setWidget(0, 0, lblIsFinished);
		layout.setWidget(0, 1, hpFinished);
		layout.setWidget(1, 0, lblTags);
		layout.setWidget(1, 1, hpTags);			
		layout.setWidget(2, 0, lblAction);
		layout.setWidget(2, 1, btnSave);
		btnSave.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				
				info.addUpdateCalendar(new Date());
				info.setTags(txtTags.getValue());
				info.setText(infoText.getHTML());
				info.setIsFinished(finished);
				InfoRemote.Util.getInstance().save(info, new AsyncCallback<KInfo>(){

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());						
					}

					@Override
					public void onSuccess(KInfo result) {
						in.setLblText(info.getGenre().getName() +": "+info.getTags());
						in.setUserObject(result);
						Kms4gwt.get().setInfo(in);
					}});
				
			}});
		
	}

	private void initCommon() {
		grid.setSize("100%","100%");
		infoText.setSize("100%","25em");		
		outer.setSize("100%","100%");
		layout.setSize("100%","100%");
				
		lblGenre.setStyleName("form-label");
		lblIsFinished.setStyleName("form-label");
		lblTags.setStyleName("form-label");
		lblInfoText.setStyleName("form-label");
		lblAction.setStyleName("form-label");
		rdoGenre.setStyleName("form-radiogroup");
		grdGenre.setStyleName("form-radiogroup");		
		txtTags.setStyleName("form-label");
		txtTags.setTitle("This could be the version of the software or content entity");
		
		
		unFinishedRadioButton.setValue(true);
		finishedRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>(){

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if(event.getValue()==true)
					finished = true;
				
			}});
		hpFinished.add(finishedRadioButton);
		hpFinished.add(unFinishedRadioButton);
		
		hpTags.add(txtTags);
		hpSubmit.add(btnSave);
		
		infoText.ensureDebugId("cwRichText-area");		
		RichTextToolbar toolbar = new RichTextToolbar(infoText);
		toolbar.ensureDebugId("cwRichText-toolbar");
		toolbar.setWidth("100%");

		// Add the components to a panel
		
		grid.setStyleName("cw-RichText");
		grid.setWidget(0, 0, toolbar);
		grid.setWidget(1, 0, infoText);
		
		
		layout.getColumnFormatter().setWidth(0, "150px");
		layout.setCellPadding(10);
		layout.setBorderWidth(1);
		layout.setStyleName("info-layout");
		outer.setSpacing(10);
		
		outer.add(layout);	
		outer.add(grid);
		initWidget(outer);		
	}

	class CustomizedHP extends HorizontalPanel {
		public CustomizedHP() {
			this.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);			
			this.setSpacing(4);
		}
	}	

}
